package org.zecontech.extraplus.datagen;

import net.minecraft.client.data.models.BlockModelGenerators;
import net.minecraft.client.data.models.ItemModelGenerators;

import net.minecraft.client.data.models.ModelProvider;
import net.minecraft.client.data.models.model.ModelTemplates;
import net.minecraft.data.PackOutput;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.SpawnEggItem;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.CropBlock;
import net.minecraft.world.level.block.FlowerPotBlock;
import net.minecraft.world.level.block.RotatedPillarBlock;
import net.minecraft.world.level.block.SaplingBlock;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.neoforged.neoforge.common.Tags;
import org.zecontech.extraplus.ExtraPlus;
import org.zecontech.extraplus.block.HerbBlock;
import org.zecontech.extraplus.init.ModBlocks;
import org.zecontech.extraplus.init.ModItems;

/**
 * 模组模型数据生成提供者
 */
public class ModModelProvider extends ModelProvider {

    public ModModelProvider(PackOutput p_388260_) {
        super(p_388260_, ExtraPlus.MOD_ID);
    }

    /**
     * 注册生成模型
     * @param blockModels 方块模型
     * @param itemModels 物品模型
     */
    @Override
    protected void registerModels(BlockModelGenerators blockModels, ItemModelGenerators itemModels) {
        ModItems.ITEMS.getEntries().forEach(item -> {
            if (item.get() instanceof SpawnEggItem spawnEggItem) {
                itemModels.generateFlatItem(spawnEggItem, ModelTemplates.FLAT_HANDHELD_ITEM);
            }else if (item.is(Tags.Items.TOOLS)) {
                itemModels.generateFlatItem(item.get(), ModelTemplates.FLAT_HANDHELD_ITEM);
            }else {
                if (!(item.get() instanceof BlockItem)) {
                    itemModels.generateFlatItem(item.get(), ModelTemplates.FLAT_ITEM);
                }
            }
        });
        ModBlocks.BLOCKS.getEntries().forEach(block -> {
            if (!(block.get() instanceof HerbBlock)
                    && !(block.get() instanceof FlowerPotBlock)
                    && !(block.get() instanceof CropBlock)
                    && !(block.get() instanceof RotatedPillarBlock)
                    && !(block.get() instanceof SaplingBlock)) {
                blockModels.createTrivialCube(block.get());
            }
        });
        registerPlants(blockModels);
        registerCrops(blockModels);
        registerTrees(blockModels);
    }

    /**
     * 注册自定义作物方块模型
     * @param blockModels 方块模型
     */
    private void registerCrops(BlockModelGenerators blockModels) {
        createCustomCropBlock(blockModels, ModBlocks.ONION.get());
        createCustomCropBlock(blockModels, ModBlocks.ETHEREAL_STRAWBERRY.get());
        createCustomCropBlock(blockModels, ModBlocks.PEPPER.get());
    }

    /**
     * 注册树木方块模型
     * @param blockModels 方块模型
     */
    private void registerTrees(BlockModelGenerators blockModels) {
        registerTree(blockModels, ModBlocks.WILLOW_LOG.get(), ModBlocks.WILLOW_WOOD.get(), ModBlocks.WILLOW_SAPLING.get(), ModBlocks.POTTED_WILLOW_SAPLING.get());
    }

    /**
     * 注册自定义树形方块模型
     * @param blockModels 方块模型
     * @param log 原木
     * @param wood 木头
     * @param sapling 树苗
     * @param pottedSapling 盆栽树苗
     */
    private void registerTree(BlockModelGenerators blockModels, Block log, Block wood, Block sapling, Block pottedSapling) {
        blockModels.woodProvider(log).logWithHorizontal(log).wood(wood);
        blockModels.createPlantWithDefaultItem(sapling, pottedSapling, BlockModelGenerators.PlantType.NOT_TINTED);
    }

    /**
     * 注册自定义花方块模型
     * @param blockModels 方块模型
     */
    private void registerPlants(BlockModelGenerators blockModels) {
        createFlowerLikeBlock(blockModels, ModBlocks.PLANTAGO.get(), ModBlocks.POTTED_PLANTAGO.get());
        createFlowerLikeBlock(blockModels, ModBlocks.ACHILLEA_WHITE.get(), ModBlocks.POTTED_ACHILLEA_WHITE.get());
        createFlowerLikeBlock(blockModels, ModBlocks.ANAGALLIS_YELLOW.get(), ModBlocks.POTTED_ANAGALLIS_YELLOW.get());
        createFlowerLikeBlock(blockModels, ModBlocks.ANTIRRHINUM.get(), ModBlocks.POTTED_ANTIRRHINUM.get());
        createFlowerLikeBlock(blockModels, ModBlocks.CAMPANULA_BLUE.get(), ModBlocks.POTTED_CAMPANULA_BLUE.get());
        createFlowerLikeBlock(blockModels, ModBlocks.CENTAUREA.get(), ModBlocks.POTTED_CENTAUREA.get());
        createFlowerLikeBlock(blockModels, ModBlocks.DIGITALIS_BLUE.get(), ModBlocks.POTTED_DIGITALIS_BLUE.get());
        createFlowerLikeBlock(blockModels, ModBlocks.DIGITALIS_YELLOW.get(), ModBlocks.POTTED_DIGITALIS_YELLOW.get());
        createFlowerLikeBlock(blockModels, ModBlocks.DOSERA.get(), ModBlocks.POTTED_DOSERA.get());
        createFlowerLikeBlock(blockModels, ModBlocks.IRIS.get(), ModBlocks.POTTED_IRIS.get());
        createFlowerLikeBlock(blockModels, ModBlocks.LEONTOPODIUM_WHITE.get(), ModBlocks.POTTED_LEONTOPODIUM_WHITE.get());
        createFlowerLikeBlock(blockModels, ModBlocks.LEUCANTHEMUM_WHITE.get(), ModBlocks.POTTED_LEUCANTHEMUM_WHITE.get());
        createFlowerLikeBlock(blockModels, ModBlocks.MUSHROOM_AMANITA_GREEN.get(), ModBlocks.POTTED_MUSHROOM_AMANITA_GREEN.get());
        createFlowerLikeBlock(blockModels, ModBlocks.MUSHROOM_BOLETUS.get(), ModBlocks.POTTED_MUSHROOM_BOLETUS.get());
        createFlowerLikeBlock(blockModels, ModBlocks.MUSHROOM_CANTHARELLUS.get(), ModBlocks.POTTED_MUSHROOM_CANTHARELLUS.get());
        createFlowerLikeBlock(blockModels, ModBlocks.MUSHROOM_GALERINA.get(), ModBlocks.POTTED_MUSHROOM_GALERINA.get());
        createFlowerLikeBlock(blockModels, ModBlocks.MUSHROOM_GYROMITRA.get(), ModBlocks.POTTED_MUSHROOM_GYROMITRA.get());
        createFlowerLikeBlock(blockModels, ModBlocks.MUSHROOM_MACROLEPIOTA.get(), ModBlocks.POTTED_MUSHROOM_MACROLEPIOTA.get());
        createFlowerLikeBlock(blockModels, ModBlocks.PAPVER_RED.get(), ModBlocks.POTTED_PAPVER_RED.get());
        createFlowerLikeBlock(blockModels, ModBlocks.TRIFOLIUM_RED.get(), ModBlocks.POTTED_TRIFOLIUM_RED.get());
        createFlowerLikeBlock(blockModels, ModBlocks.TRIFOLIUM_WHITE.get(), ModBlocks.POTTED_TRIFOLIUM_WHITE.get());
    }

    /**
     * 创建花草类型方块模型
     * @param blockModels 方块模型
     * @param block 方块
     * @param pottedBlock 盆栽方块
     */
    protected void createFlowerLikeBlock(BlockModelGenerators blockModels, Block block, Block pottedBlock) {
        blockModels.createPlantWithDefaultItem(block,
                pottedBlock,
                BlockModelGenerators.PlantType.NOT_TINTED);
    }

    /**
     * 创建自定义作物方块模型
     * @param blockModels 方块模型
     * @param cropBlock 自定义作物方块
     */
    private void createCustomCropBlock(BlockModelGenerators blockModels, Block cropBlock) {
        blockModels.createCropBlock(cropBlock, BlockStateProperties.AGE_7, 0, 1, 2, 3, 4, 5, 6, 7);
    }
}
